package ui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;

import ui.ScenarioOriginatorHostComponent.HostPartToolbar;
import ui.model.TableListModel;
import databaseConnect.HostConfigurationRequestProcessor;
import databaseConnect.HostConfigurationUpdateOrInsertProcessor;
import databaseConnect.ScenarioConfigurationRequestProcessor;

public class ScenarioOriginiatorHostComponentHostSelectionListContainer extends JPanel{

	private HostSelToolbar hst = new HostSelToolbar();
	private BorderLayout layout = new BorderLayout();

	private ScenarioOriginatorHostComponentSplitPane parent = null;
	//for selection available Hosts
	private JTable HostSelectionList =null;
	public ScenarioOriginiatorHostComponentHostSelectionListContainer(ScenarioOriginatorHostComponentSplitPane par){
		super();
		parent = par;
		this.setLayout(layout);
		this.add(hst, BorderLayout.NORTH);
		JScrollPane mySel = new JScrollPane(this.getHostSelectionListFromDB());
		this.add(mySel, BorderLayout.CENTER);
	}
	private JTable getHostSelectionListFromDB() {
		if (HostSelectionList == null) {
			HostSelectionList = new JTable(new TableListModel(ScenarioConfigurationRequestProcessor.getHostSelectionRows(), ScenarioConfigurationRequestProcessor.getHostSelectionRowColumnNames()));
		}
		return HostSelectionList;
	}

	public JTable getHostSelectionList() {
		if (HostSelectionList != null && HostSelectionList.getSelectedRow() < 0){
			HostSelectionList.changeSelection(0, 0, false, false);
		}
		return HostSelectionList;
	}

	public String getHostIdFromSelectedRow(int selectedRowID){
		return (String)((TableListModel) HostSelectionList.getModel()).getValueAt(selectedRowID, 0);
	}
	
	public int getHostPosXFromSelectedRow(int selectedRowID){
		try {
			int tmp = Integer.parseInt((String)((TableListModel) HostSelectionList.getModel()).getValueAt(selectedRowID, 1));
		return tmp;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}

	public int getHostPosYFromSelectedRow(int selectedRowID){
		try {
			int tmp = Integer.parseInt((String)((TableListModel) HostSelectionList.getModel()).getValueAt(selectedRowID, 2));
		return tmp;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}

	public void addNewHost(int id, int xpos, int ypos, int active) {
		Vector<String> tmp = new Vector<String>();
		tmp.add(id + "");
		tmp.add(xpos + "");
		tmp.add(ypos + "");
		tmp.add(active + "");
	((TableListModel)getHostSelectionList().getModel()).addRow(tmp);
	HostConfigurationUpdateOrInsertProcessor.setHostParameterRow(tmp);
	getHostSelectionList().updateUI();
	}

	protected void newRandomHost(){
		int maxWidth = parent.getMiniworldWidth();
		int maxHeight = parent.getMiniworldHeight();
		int newx = (int)(Math.random() * maxWidth);
		int newy = (int)(Math.random() * maxHeight);
		int newid = HostConfigurationRequestProcessor.getLastRowIDFromHostDescriptionTable()+1;
		int newActive = (int)(Math.random()*2);
		addNewHost(newid, newx, newy, newActive);
	}

	class HostSelToolbar extends JToolBar {
		private JButton newRandomHost = null;
		private String newRandomHostButtonTitle = "New Random Host";

		private JButton getNewRandomHostButton() {
			if (newRandomHost == null) {
				newRandomHost = new JButton(newRandomHostButtonTitle);
				newRandomHost.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent arg0) {
						newRandomHost();
					}

				});
			}
			return newRandomHost;
		}

		// Methods
		public HostSelToolbar() {
			super();
			this.add(this.getNewRandomHostButton());
		}

	}

}
